package com.eloo.otsclient.core;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebClientOptions;
import com.gargoylesoftware.htmlunit.WebWindowEvent;
import com.gargoylesoftware.htmlunit.WebWindowListener;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.Cookie;

public class OTSWebClient {
	private static WebClient webClient;  
//	private OTSAttribute otsAttrbitute; 
	protected static Log logger= LogFactory.getLog(Thread.currentThread().getClass());

	public OTSWebClient(){

		webClient = new WebClient(BrowserVersion.FIREFOX_10);
		WebClientOptions webClientOptions = webClient.getOptions();
		webClientOptions.setThrowExceptionOnScriptError(false);
		webClientOptions.setThrowExceptionOnFailingStatusCode(false);
		webClientOptions.setJavaScriptEnabled(true);
		webClientOptions.setRedirectEnabled(true);
		webClientOptions.setUseInsecureSSL(true);
		webClient.setAjaxController(new NicelyResynchronizingAjaxController());
		webClient.waitForBackgroundJavaScript(30000);
		// CookieManager cmk= new CookieManager();
		// cmk.setCookiesEnabled(true);
		// cmk.clearCookies();
		// [JSESSIONID=C1935A168ACD4D08646C9661C107AAFF;domain=;path=/otsweb,
		// BIGipServerotsweb=2228486410.48160.0000;domain=dynamic.12306.cn;path=/]
		// cmk.addCookie(new Cookie("dynamic.12306.cn","JSESSIONID",
		// "398514DD959E8F24266730444F2115E6"));
		// cmk.addCookie(new Cookie("dynamic.12306.cn","BIGipServerportal",
		// "2949906698.17695.0000"));
		// / cmk.addCookie(new Cookie("dynamic.12306.cn","BIGipServerotsweb",
		// "2513699082.48160.0000"));
		// cmk.addCookie(new Cookie("dynamic.12306.cn","BIGipServertrainquery",
		// "2379809034.22560.0000"));
		//cookm = webClient.getCookieManager();
		// webClient.setCookieManager(cmk); 
		webClient.addWebWindowListener(new WebWindowListener() {
			public void webWindowOpened(WebWindowEvent event) 
			{
				 HtmlPage	confirmPassengerAction = (HtmlPage) event.getWebWindow().getEnclosedPage();  
			 	 if(confirmPassengerAction!=null && confirmPassengerAction.asText().contains("提交订单确认")){
			 		 logger.info(confirmPassengerAction.asText());
				  DomNodeList<DomElement>   ofbuton=confirmPassengerAction.getElementsByTagName("button");
				  
				 
				  if(ofbuton.size()>0){
					  logger.info("有订票按钮！"+(ofbuton)); 
				  }
				  System.out.println("confirmPassengerAction"+ confirmPassengerAction.asXml());
				 
				  HtmlButton submitbutton=(HtmlButton) confirmPassengerAction.getElementsByTagName("button").get(1); 
				  submitbutton.removeAttribute("disabled");
				  submitbutton.removeAttribute("class");
				  logger.info("submitbutton"+ submitbutton.asXml());
				  HtmlPage ordersuccess;
				  try {
					ordersuccess = (HtmlPage)	submitbutton.click();
			
				  logger.info("订票结束！"+((HtmlPage)ordersuccess).asXml()); 
				  } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			 	 }
				/*
				otsAttrbitute=new OTSAttribute();
				otsAttrbitute.getWebWindowLinks().add(event.getWebWindow()); 
			*/}

			public void webWindowContentChanged(WebWindowEvent event) {
				
			}

			public void webWindowClosed(WebWindowEvent event) {

			}

		});
	}
	public static WebClient getRootWebClient() { 
		 
		 return webClient; 
	}
	public static WebClient getCloneWebClient(){
		
		WebClient	clonewebClient = new WebClient(webClient.getBrowserVersion());  
		WebClientOptions webClientOptions = clonewebClient.getOptions();
		webClientOptions.setThrowExceptionOnScriptError(false);
		webClientOptions.setThrowExceptionOnFailingStatusCode(false);
		webClientOptions.setJavaScriptEnabled(true);
		webClientOptions.setRedirectEnabled(true);
		webClientOptions.setUseInsecureSSL(true);
		clonewebClient.setAjaxController(new NicelyResynchronizingAjaxController());
		clonewebClient.waitForBackgroundJavaScript(30000);  
		CookieManager	oldcook = webClient.getCookieManager();
		
		CookieManager cmk= new CookieManager(); 
	/*	 [JSESSIONID=C1935A168ACD4D08646C9661C107AAFF;domain=;path=/otsweb,
		 BIGipServerotsweb=2228486410.48160.0000;domain=dynamic.12306.cn;path=/]*/
		 cmk.addCookie(oldcook.getCookie("JSESSIONID"));
	//	 cmk.addCookie(new Cookie("dynamic.12306.cn","BIGipServerportal", oldcook.getCookie("BIGipServerportal").toString()));
		 cmk.addCookie(oldcook.getCookie("BIGipServerotsweb"));
	//	 cmk.addCookie(new Cookie("dynamic.12306.cn","BIGipServertrainquery", "2379809034.22560.0000"));
	 
		clonewebClient.setCookieManager(cmk);
		/*clonewebClient.addWebWindowListener(new WebWindowListener() {
			public void webWindowOpened(WebWindowEvent event) 
			{
				 HtmlPage	confirmPassengerAction = (HtmlPage) event.getWebWindow().getEnclosedPage();  
			 	 if(confirmPassengerAction!=null && confirmPassengerAction.asText().contains("提交订单确认")){
			 	 logger.info(confirmPassengerAction.asText());
				  DomNodeList<DomElement>   ofbuton=confirmPassengerAction.getElementsByTagName("button");
				 
				  if(ofbuton.size()>0){
					  logger.info("有订票按钮！"+(ofbuton)); 
				  }
				  System.out.println("confirmPassengerAction"+ confirmPassengerAction.asXml());
				 
				  HtmlButton submitbutton=(HtmlButton) confirmPassengerAction.getElementsByTagName("button").get(1); 
				  submitbutton.removeAttribute("disabled");
				  submitbutton.removeAttribute("class");
				  logger.info("submitbutton"+ submitbutton.asXml());
				  HtmlPage ordersuccess;
				  try {
					ordersuccess = (HtmlPage)	submitbutton.click(); 
				  logger.info("订票结束！"+((HtmlPage)ordersuccess).asXml()); 
				  } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
			 	}
				
				otsAttrbitute=new OTSAttribute();
				otsAttrbitute.getWebWindowLinks().add(event.getWebWindow()); 
			}

			@Override
			public void webWindowContentChanged(WebWindowEvent event) {
			}

			@Override
			public void webWindowClosed(WebWindowEvent event) {

			}

		});
		 */
		return clonewebClient;

	}
/*	public OTSAttribute getOtsAttrbitute() {
		return otsAttrbitute;
	}

	public void setOtsAttrbitute(OTSAttribute otsAttrbitute) {
		this.otsAttrbitute = otsAttrbitute;
	}*/
	
	public WebClient getWebClient() {
		return webClient;
	}

	public void setWebClient(WebClient webClient) {
		OTSWebClient.webClient = webClient;
	}


	
	
	public static void main(String ags[]){
		 
		   System.out.println(OTSWebClient.getRootWebClient().getCookieManager());
		   System.out.println(OTSWebClient.getCloneWebClient().getCookieManager());
		   System.out.println(OTSWebClient.getCloneWebClient().getCookieManager());
		 
	}
	
	
}
